

#include <mtx_util.h>
#include <matrix.h>
#include <errno.h>
#include <stdio.h>

extern char comp_readable;
extern char new_file;
extern char *new_file_name;

int mult_matrices(vm_matrix **mtx, int len){

  if (len < 1) return 0;

  vm_matrix *tmp = vm_matrix_mult(mtx[0], mtx[1]);

  int i = 2;
  for ( ; i <= len; i++){
    vm_matrix *old = tmp;
    tmp = vm_matrix_mult(tmp, mtx[i]);
    vm_free_matrix(old);
  }
  
  if (new_file){
    FILE *nf = fopen(new_file_name, "w");
    if (nf == NULL){
      perror("Could not open new file");
      return 0;
    }
    if (comp_readable){
      vm_fprint_cr_matrix(nf, tmp);
    } else {
      vm_fprint_matrix(nf, tmp, " ");
    }
  } else {
    printf("Mutiplication Resultant Matrix:\n");
    vm_print_matrix(tmp, "  ");
  }
  vm_free_matrix(tmp);

  return 1;

}
